package com.echo.usedsfu;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.echo.usedsfu.entity.MyImage;
import com.echo.usedsfu.persistence.PMF;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class CheckImgServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(CheckImgServlet.class.getName());
	
	public void doPost(HttpServletRequest req,  HttpServletResponse resp)throws IOException{
		try{
			String img_id = req.getParameter("img_id");
//			if(img_id.equals("null")){
//				resp.sendRedirect("/index.jsp");
//			}
			PersistenceManager pm = PMF.get().getPersistenceManager();
			Key k = KeyFactory.createKey(MyImage.class.getSimpleName(), Long.parseLong(img_id));
//			List<MyImage> results = (List<MyImage>)pm.newQuery(query).execute();
			Blob image = ((MyImage)pm.getObjectById(MyImage.class, k)).getImage();
			
			resp.setContentType("image/jpeg");
			resp.getOutputStream().write(image.getBytes());
		}catch(IOException e){
			e.printStackTrace();
		}catch(NumberFormatException e){
			resp.sendRedirect("/index.jsp");
		}
	}
}
